package com.bsoft.gol.hcs.clinical.dao;

import ctd.persistence.annotation.DAOMethod;
import ctd.persistence.annotation.DAOParam;
import ctd.persistence.support.hibernate.HibernateSupportDelegateDAO;
import ctd.persistence.support.hibernate.template.AbstractHibernateStatelessResultAction;
import ctd.persistence.support.hibernate.template.HibernateSessionTemplate;
import ctd.persistence.support.hibernate.template.HibernateStatelessResultAction;
import ctd.util.annotation.RpcService;
import ctd.util.converter.ConversionUtils;
import hcn.base.CheckRequisitionDetail;
import org.hibernate.Query;
import org.hibernate.StatelessSession;
import org.hibernate.criterion.CriteriaSpecification;
import pcn.prescribe.DisposalExpenseDetail;
import com.bsoft.gol.hcs.utils.CommonUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public abstract class DisposalExpenseDetailDAO extends HibernateSupportDelegateDAO<DisposalExpenseDetail> {

    @DAOMethod(sql = " from DisposalExpenseDetail where expenseId =:expenseId ")
    public abstract List<DisposalExpenseDetail> queryDisposalExpenseDetail(@DAOParam("expenseId") String expenseId);

    @DAOMethod(sql = "delete from DisposalExpenseDetail where expenseId=:expenseId")
    public abstract void deleteDisposalExpenseDetail(@DAOParam(value = "expenseId") String expenseId);

    @RpcService
    public List<CheckRequisitionDetail> queryDisposalExpenseDetailByCheckId(final String checkRequisitionRecordId){
        HibernateStatelessResultAction<List<CheckRequisitionDetail>> action = new AbstractHibernateStatelessResultAction<List<CheckRequisitionDetail>>() {
            @Override
            public void execute(StatelessSession ss) throws Exception {
                StringBuffer sql = new StringBuffer();
                sql.append("SELECT " +
                        " c.item_id AS itemId, " +
                        " c.item_name AS itemName, " +
                        " c.number, " +
                        " c.price, " +
                        " c.amount, " +
                        " c.unit " +
                        "FROM " +
                        " check_requisition_record a, " +
                        " disposal_expense_record b, " +
                        " disposal_expense_detail c " +
                        "WHERE " +
                        " a.check_requisition_record_id =:checkRequisitionRecordId " +
                        "AND a.application_no = b.application_no " +
                        "AND b.expense_id = c.expense_id");
                Query query = ss.createSQLQuery(sql.toString());
                query.setParameter("checkRequisitionRecordId", checkRequisitionRecordId);
                query.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
                List<Map> list = query.list();
                if (CommonUtils.isNotEmpty(list)) {
                    List<CheckRequisitionDetail> checkRequisitionDetails = new ArrayList<>();
                    for (Map map : list) {
                        CheckRequisitionDetail queryMedicalSkillTotalResp = ConversionUtils.convert(map, CheckRequisitionDetail.class);
                        checkRequisitionDetails.add(queryMedicalSkillTotalResp);
                    }
                    setResult(checkRequisitionDetails);
                }else {
                    setResult(null);
                }
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }
}
